<template>
  <div v-loading="loading">
    <el-checkbox-group v-for="item in formThead" :key="item.id" v-model="checkboxVal">
      <el-checkbox :label="item">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-button type="primary" @click="save">保存</el-button>

  </div>
</template>

<script>
import Bus from '@/utils/bus'

import { getList } from '@/api/system/role'

export default {
  data() {
    return {
      loading: true,
      checkboxVal: [],
      formThead: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getList().then(response => {
        this.formThead = response.data
        if (!this.$store.state.edit.user.roleIds) {
          this.loading = false
          return
        }
        this.formThead.forEach(element => {
          if (this.$store.state.edit.user.roleIds.indexOf(element.id) >= 0) {
            this.checkboxVal.push(element)
          }
        })
        this.loading = false
      })
    },
    save() {
      console.log('save')

      this.$store.state.edit.user.roleList = this.checkboxVal
      Bus.$emit('selectRoleClose')
    }
  }
}
</script>

